package ink.yql.service.Impl;

import ink.yql.dao.ProjectDao;
import ink.yql.dao.SolveLockDao;
import ink.yql.model.Project;
import ink.yql.model.SolveLock;
import ink.yql.service.SolveLockService;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author YQL
 */
@Service
@Transactional
public class SolveLockServiceImpl implements SolveLockService {
    @Autowired
    private SolveLockDao solveLockDao;
    @Autowired
    private ProjectDao projectDao;

    @Override
    public RestObject getByPid(Integer pid) {
        // 数据校验
        if (pid == null) {
            return RestObject.builder().code("400").message("请传输一个正确的数据").build();
        }
        List<SolveLock> list = solveLockDao.getByPid(pid);
        return RestObject.builder().code("200").message("查询解锁条件成功").data(list).build();
    }

    @Override
    public RestObject addOne(SolveLock solveLock) {
        // 校验数据
        if (solveLock == null) {
            return RestObject.builder().code("400").message("请传输一个正确的数据").build();
        }
        Project project;
        if (solveLock.getPid() == null || (project = projectDao.getByPid(solveLock.getPid())) == null) {
            return RestObject.builder().code("404").message("项目不存在").build();
        }
        // 解锁金额要高于项目所需金额
        if (project.getPtargetMoney() >= solveLock.getSlmoney()){
            return RestObject.builder().code("400").message("解锁金额要大于项目所需金额").build();
        }
        solveLockDao.addOne(solveLock);
        return RestObject.builder().code("200").message("解锁条件已添加").build();
    }
}
